<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="####1. 蓝牙发送照片1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="iOS-Bluetooth(蓝牙)">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/iOS-Special-Function/iOS-Bluetooth(蓝牙)/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="####1. 蓝牙发送照片1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2018-08-06T10:53:55.798Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="iOS-Bluetooth(蓝牙)">
<meta name="twitter:description" content="####1. 蓝牙发送照片1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/iOS-Special-Function/iOS-Bluetooth(蓝牙)/"/>





  <title>iOS-Bluetooth(蓝牙) | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/iOS-Special-Function/iOS-Bluetooth(蓝牙)/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">iOS-Bluetooth(蓝牙)</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:38+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>####1. 蓝牙发送照片<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br></pre></td><td class="code"><pre><span class="line">#import &quot;ViewController.h&quot;</span><br><span class="line">#import &lt;GameKit/GameKit.h&gt;</span><br><span class="line"></span><br><span class="line">@interface ViewController ()&lt;UINavigationControllerDelegate, UIImagePickerControllerDelegate, GKPeerPickerControllerDelegate&gt;</span><br><span class="line"></span><br><span class="line">@property (weak, nonatomic) IBOutlet UIImageView *imageView;</span><br><span class="line"></span><br><span class="line">/** 会话类*/</span><br><span class="line">@property (nonatomic, strong) GKSession *session;</span><br><span class="line"></span><br><span class="line">@end</span><br><span class="line"></span><br><span class="line">@implementation ViewController</span><br><span class="line"></span><br><span class="line">- (void)viewDidLoad &#123;</span><br><span class="line">    [super viewDidLoad];</span><br><span class="line">    // Do any additional setup after loading the view, typically from a nib.</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#pragma mark 连接设备</span><br><span class="line">- (IBAction)connectClick:(id)sender &#123;</span><br><span class="line">    //1. 创建GKPeerPickerController连接控制器</span><br><span class="line">    GKPeerPickerController * picker = [GKPeerPickerController new];</span><br><span class="line">    </span><br><span class="line">    //2. 设置代理 --&gt; 获取数据</span><br><span class="line">    picker.delegate = self;</span><br><span class="line">    </span><br><span class="line">    //3. 显示控制器 --&gt; show 此控制器和AlertView很像, 不是全屏的, 不用push modal</span><br><span class="line">    [picker show];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#pragma mark GKPeerPickerController 代理方法</span><br><span class="line">/**</span><br><span class="line"> 此方法在连接到另一台设备时, 会调用</span><br><span class="line"> peerID: 另一台设备的ID</span><br><span class="line"> session: 会话类, 用于接收和发送数据</span><br><span class="line"> */</span><br><span class="line">- (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session</span><br><span class="line">&#123;</span><br><span class="line">    //1. 保留session</span><br><span class="line">    self.session = session;</span><br><span class="line">    </span><br><span class="line">    //2. 设置句柄 (设置代理) --&gt; 将来一旦受到数据, 将由句柄的方法来处理数据</span><br><span class="line">    // 一旦设置了句柄, 那么还需要实现另一个方法</span><br><span class="line">    [self.session setDataReceiveHandler:self withContext:nil];</span><br><span class="line">    </span><br><span class="line">    //3. 消失控制器</span><br><span class="line">    [picker dismiss];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">// 一旦设置了句柄, 还需要实现此方法</span><br><span class="line">#pragma mark 接收到数据的时候, 会调用此方法来处理</span><br><span class="line">- (void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context</span><br><span class="line">&#123;</span><br><span class="line">    //1. 将Data转换成image对象</span><br><span class="line">    UIImage *image = [UIImage imageWithData:data];</span><br><span class="line">    </span><br><span class="line">    //2. 然后设置到界面上</span><br><span class="line">    self.imageView.image = image;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#pragma mark 发送照片方法</span><br><span class="line">- (IBAction)sendPhotoClick:(id)sender &#123;</span><br><span class="line">    //1. 将image转换成Data</span><br><span class="line">    NSData *data = UIImageJPEGRepresentation(self.imageView.image, 0.5);</span><br><span class="line">    </span><br><span class="line">    //2. 使用会话类发送数据</span><br><span class="line">    /**</span><br><span class="line">     GKSendDataReliable,     如果发送失败, 会重新发送, 直到成功</span><br><span class="line">     GKSendDataUnreliable,   发送一次就不管了</span><br><span class="line">     */</span><br><span class="line">    [self.session sendDataToAllPeers:data withDataMode:GKSendDataReliable error:nil];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#pragma mark 选择照片</span><br><span class="line">- (IBAction)selectPhontoClick:(id)sender &#123;</span><br><span class="line">    //1. 判断是否可用</span><br><span class="line">    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) &#123;</span><br><span class="line">        return;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    //2. 创建UIImagePickerController</span><br><span class="line">    UIImagePickerController *picker = [UIImagePickerController new];</span><br><span class="line">    </span><br><span class="line">    //3. 设置类型</span><br><span class="line">    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;</span><br><span class="line">    </span><br><span class="line">    //4. 设置代理</span><br><span class="line">    picker.delegate = self;</span><br><span class="line">    </span><br><span class="line">    //5. 模态视图弹出</span><br><span class="line">    [self presentViewController:picker animated:YES completion:nil];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#pragma mark UIImagePickerController 代理方法</span><br><span class="line">- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary&lt;NSString *,id&gt; *)info</span><br><span class="line">&#123;</span><br><span class="line">    //1. 获取照片并显示到界面上</span><br><span class="line">    self.imageView.image = info[UIImagePickerControllerOriginalImage];</span><br><span class="line">    </span><br><span class="line">    //2. 关闭控制器</span><br><span class="line">    [picker dismissViewControllerAnimated:YES completion:nil];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">@end</span><br></pre></td></tr></table></figure></p>
<a id="more"></a>
<p>####2. CoreBluetooth<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br></pre></td><td class="code"><pre><span class="line">#import &quot;ViewController.h&quot;</span><br><span class="line">#import &lt;CoreBluetooth/CoreBluetooth.h&gt;</span><br><span class="line"></span><br><span class="line">@interface ViewController ()&lt;CBCentralManagerDelegate, CBPeripheralDelegate&gt;</span><br><span class="line"></span><br><span class="line">/** 中央管理者*/</span><br><span class="line">@property (nonatomic, strong) CBCentralManager *mgr;</span><br><span class="line"></span><br><span class="line">/** 扫描到的外围设备的数据数组*/</span><br><span class="line">@property (nonatomic, strong) NSMutableArray *peripheralArray;</span><br><span class="line"></span><br><span class="line">@end</span><br><span class="line"></span><br><span class="line">@implementation ViewController</span><br><span class="line"></span><br><span class="line">- (NSMutableArray *)peripheralArray</span><br><span class="line">&#123;</span><br><span class="line">    if (_peripheralArray == nil) &#123;</span><br><span class="line">        _peripheralArray = [NSMutableArray array];</span><br><span class="line">    &#125;</span><br><span class="line">    return _peripheralArray;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (void)viewDidLoad &#123;</span><br><span class="line">    [super viewDidLoad];</span><br><span class="line">    </span><br><span class="line">    //1. 建立中央管理者</span><br><span class="line">    //queue: 如果传空, 就代表着在主队列</span><br><span class="line">    self.mgr = [[CBCentralManager alloc] initWithDelegate:self queue:nil];</span><br><span class="line">    </span><br><span class="line">    //2. 扫描周边设备</span><br><span class="line">    //Services: 是服务的UUID, 而且是个数据. 如果不穿, 默认扫描全部服务</span><br><span class="line">    [self.mgr scanForPeripheralsWithServices:nil options:nil];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#pragma mark - CBCentralManager 代理方法</span><br><span class="line">#pragma mark 必须调用的代理方法</span><br><span class="line">- (void)centralManagerDidUpdateState:(CBCentralManager *)central</span><br><span class="line">&#123;</span><br><span class="line">    </span><br><span class="line">    NSLog(@&quot;state: %zd&quot;,central.state);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#pragma mark 当发现外围设备时, 会调用的方法</span><br><span class="line">/**</span><br><span class="line"> Peripheral: 外围设备</span><br><span class="line"> Data : 相关的数据</span><br><span class="line"> RSSI : 信号强度</span><br><span class="line"> */</span><br><span class="line">- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary&lt;NSString *,id&gt; *)advertisementData RSSI:(NSNumber *)RSSI</span><br><span class="line">&#123;</span><br><span class="line">    //3. 记录扫描到的设备</span><br><span class="line">    if (![self.peripheralArray containsObject:peripheral]) &#123;</span><br><span class="line">        [self.peripheralArray addObject:peripheral];</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    // 隐藏的步骤: 你应该搞一个列表给用户选择, 让用户自己选择要连接到哪一个设备</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#pragma mark 连接扫描到的设备 --&gt; 此方法是咱们自己写的, 用户当选中了设备时,应该调用此方法</span><br><span class="line">- (void)connectPeripheral:(CBPeripheral *)peripheral</span><br><span class="line">&#123;</span><br><span class="line">    //4. 连接外围设备</span><br><span class="line">    [self.mgr connectPeripheral:peripheral options:nil];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#pragma mark 此方法是连接到外设时会调用的代理方法</span><br><span class="line">- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral</span><br><span class="line">&#123;</span><br><span class="line">#warning</span><br><span class="line">    //5. 设置外围设备的代理 --&gt; 一旦连接外设, 那么将有外设来管理服务和特征的处理</span><br><span class="line">    peripheral.delegate = self;</span><br><span class="line">    //6. 扫描服务 --&gt; 可以传入UUID</span><br><span class="line">    // 传空, 代表扫描所有服务</span><br><span class="line">    [peripheral discoverServices:nil];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#pragma mark 外设的代理方法 当发现到服务的时候会调用</span><br><span class="line">- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error</span><br><span class="line">&#123;</span><br><span class="line">    //7. 获取指定的服务, 然后根据此服务来查找特征</span><br><span class="line">    //services : 外设的所有服务, 会保存在一个services中</span><br><span class="line">    for (CBService *service in peripheral.services) &#123;</span><br><span class="line">        //加入我们的服务的UUID是 &quot;123&quot;</span><br><span class="line">        if ([service.UUID.UUIDString isEqualToString:@&quot;123&quot;]) &#123;</span><br><span class="line">            </span><br><span class="line">            //如果UUID一致, 则开始扫描特征</span><br><span class="line">            [peripheral discoverCharacteristics:nil forService:service];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#pragma mark 外设的代理方法 当发现到特征的时候会调用</span><br><span class="line">- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error</span><br><span class="line">&#123;</span><br><span class="line">    //8. 获取指定的特征, 然后根据此特征, 才能根据自己的需求进行数据交互处理</span><br><span class="line">    //char&lt;#(nonnull CBCharacteristic *)#&gt;acteristics : 服务的数组中, 会包含在一个characteristics的数组</span><br><span class="line">    for (CBCharacteristic *characteristic in service.characteristics) &#123;</span><br><span class="line">        //假如我们的特征的UUID是 &quot;456&quot;</span><br><span class="line">        if ([characteristic.UUID.UUIDString isEqualToString:@&quot;456&quot;]) &#123;</span><br><span class="line">            </span><br><span class="line">            // 如果获取到了指定的特征, 则可以进行数据交互处理</span><br><span class="line">           </span><br><span class="line">            //[peripheral readValueForCharacteristic:characteristic];</span><br><span class="line">           // peripheral writeValue:&lt;#(nonnull NSData *)#&gt; forCharacteristic: type:&lt;#(CBCharacteristicWriteType)#&gt;</span><br><span class="line">            </span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    //</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#pragma mark 断开连接</span><br><span class="line">- (void)viewWillDisappear:(BOOL)animated</span><br><span class="line">&#123;</span><br><span class="line">    [super viewWillDisappear:animated];</span><br><span class="line">    //9. 断开连接</span><br><span class="line">    [self.mgr stopScan];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">@end</span><br></pre></td></tr></table></figure></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/iOS-Special-Function/iOS-Audio(音频)/" rel="next" title="iOS-Audio(音频)">
                <i class="fa fa-chevron-left"></i> iOS-Audio(音频)
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/iOS-Special-Function/iOS-ContactsUI(联系人)/" rel="prev" title="iOS-ContactsUI(联系人)">
                iOS-ContactsUI(联系人) <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
